package jiemian1;

import gonggong.User;
import gonggong.mySql_Driver;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.Socket;
import java.sql.SQLException;

public class findpass {
    private JComboBox select1;
    private JTextField txtanswer;
    private JComboBox select2;
    private JLabel problem1;
    private JLabel answer1;
    private JLabel problem2;
    private JLabel answer2;
    private JTextField txtanswer2;
    public JPanel find;
    private JButton surebtn;
    private JButton deletebtn;
    private JTextField idtxt;
    private JLabel id;
    private static String userId;
    private static String answ1;
    private static String answ2;
    public static JFrame frame1 = new JFrame("验证密保问题");
    mySql_Driver mysql = new mySql_Driver();
    User user[];
    int count;

    public findpass() {
        surebtn.addActionListener(new ActionListener() {
            Socket s;
            @Override
            public void actionPerformed(ActionEvent e) {
                // 如果正确进入修改密码页面
                JFrame frame=new JFrame("设置新密码");
                frame.setContentPane(new changepass(frame).change);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setBounds(((Toolkit.getDefaultToolkit().getScreenSize().width) / 2) - 300,
                        ((Toolkit.getDefaultToolkit().getScreenSize().height) / 2) - 300, 600, 600);
                frame.pack();
                frame.setVisible(true);
                frame.setSize(500,300);
                frame1.dispose();
            }
        });
        deletebtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //取消按钮，关闭验证页面
                frame1.dispose();
            }
        });
    }

    public findpass(JFrame frame) {
        frame1=frame;
        try {
            user= mysql.read();
            count = mysql.getCount();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        surebtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                boolean flag =false;
                userId=idtxt.getText();
                answ1=txtanswer.getText();
                answ2=txtanswer2.getText();
                //点击确认，与数据库对比如果错误，提示重新回答，
                for(int i=1;i<=count;i++){
                    if(user[i].getUserId().equals(userId)){
                        if(user[i].getAnswer1().equals(answ1)&&user[i].getAnswer2().equals(answ2)){
                            flag = true;
                            break;
                        }else{
                            break;
                        }
                    }
                }
                if(!flag){
                    JOptionPane.showMessageDialog(null,"密保错误");
                    return ;
                }
                // 如果正确进入修改密码页面
                JFrame frame=new JFrame("设置新密码");
                frame.setContentPane(new changepass(frame).change);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setBounds(((Toolkit.getDefaultToolkit().getScreenSize().width) / 2) - 300,
                        ((Toolkit.getDefaultToolkit().getScreenSize().height) / 2) - 300, 600, 600);
                frame.pack();
                frame.setVisible(true);
                frame.setSize(500,300);
                frame1.dispose();

            }
        });
        deletebtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                //取消按钮，关闭验证页面
                frame1.dispose();
            }
        });
    }

    public static void main(String[] args) {

        frame1.setContentPane(new findpass().find);
        frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame1.setBounds(((Toolkit.getDefaultToolkit().getScreenSize().width) / 2) - 300,
                ((Toolkit.getDefaultToolkit().getScreenSize().height) / 2) - 300, 600, 600);
        frame1.pack();
        frame1.setVisible(true);
        frame1.setSize(500,350);
    }
}
